home *** CD-ROM | disk | FTP | other *** search
/ Aminet 25 / Aminet 25 (1998)(GTI - Schatztruhe)[!][Jun 1998].iso / Aminet / util / cli / ksc_GrepMem.lha / search.asm < prev   
Encoding:
Assembly Source File  |  1998-04-24  |  2.4 KB  |  123 lines

  1. ;--- Init routine -----------------------------------------------------------
  2.  
  3. Init    clr.l    hitcnt(a5)    ; hits = 0
  4.     clr.w    column(a5)    ; start at column 2 on 1st line
  5.  
  6.     lea    padpos(pc),a0    ; set up pad character
  7.     move.b    #"-",(a0)
  8.     tst.l    padzero(a5)
  9.     beq.s    1$
  10.     move.b    #"0",(a0)
  11. 1$
  12.  
  13. ; set up start and end addresses
  14.  
  15.     move.l    begaddr(a5),d0
  16.     bne.s    .got
  17.     clr.l    start(a5)
  18.     move.l    4.w,a0
  19.     move.l    MaxLocMem(a0),end(a5)
  20.     bra    Search
  21.  
  22. .got    move.l    d0,a0
  23.     bsr.s    hexconv
  24.     tst.l    d1
  25.     beq.s    .nhex
  26.     move.l    d0,start(a5)
  27.  
  28.     move.l    endaddr(a5),d0
  29.     beq.s    .noend
  30.     move.l    d0,a0
  31.     bsr.s    hexconv
  32.     tst.l    d1
  33.     beq.s    .nhex
  34.     move.l    d0,end(a5)
  35.     bra    Search
  36.  
  37. .nhex    error    BAD_NUMBER
  38.     rts
  39. .noend    error    REQUIRED_ARG_MISSING
  40.     rts
  41.  
  42. hexconv    include    hex.asm
  43.  
  44. ;--- Search routine ---------------------------------------------------------
  45.  
  46. Search    move.l    start(a5),a0
  47.     move.l    end(a5),a1
  48.     cmp.l    a0,a1
  49.     beq.s    .cleanup    ; if start=end then exit
  50.     bhi.s    .noswap        ; if start>end then swap start and end
  51.     exg.l    a0,a1
  52. .noswap    move.l    a0,start(a5)
  53.     move.l    a1,end(a5)
  54.  
  55.     move.l    searchstr(a5),str(a5)
  56.     print    begin(pc),str(a5)
  57.  
  58. ; search routine
  59. ; a4=string to search for
  60. ; a3=current position in memory (outer)
  61. ; a2=current position in memory (inner)
  62.  
  63.     move.l    start(a5),a3
  64.     move.l    searchstr(a5),a4
  65.     tst.b    (a4)        ; if string = '' then exit
  66.     beq.s    .cleanup
  67. .next_outer
  68.     move.b    (a4),d0
  69.     cmp.b    (a3)+,d0
  70.     bne.s    .no_start
  71.     lea    1(a4),a1    ; a1=2nd byte of search string
  72.     move.l    a3,a2        ; a2=2nd byte of matched memory
  73. .next_inner
  74.     tst.b    (a1)        ; if search string ends during inner loop
  75.     beq.s    .matched_whole    ; then we must have matched fully
  76.     cmp.b    (a1)+,(a2)+
  77.     beq.s    .next_inner
  78. .return
  79. .no_start
  80.     cmp.l    end(a5),a3
  81.     bne.s    .next_outer
  82.  
  83. .cleanup
  84.     print    done(pc),hitcnt(a5)
  85.     rts
  86.  
  87. .matched_whole
  88.     cmp.l    a3,a4
  89.     beq.s    1$        ; don't report own copy
  90.     cmp.b    #'"',-1(a3)    ; don't report if begins with quotes
  91.     beq.s    1$
  92.  
  93.     ; we got a match!
  94.  
  95.     move.l    a3,address(a5)
  96.     print    hit(pc),address(a5)
  97.     addq.l    #1,hitcnt(a5)
  98.  
  99.     ; check for CTRL-C and die if neccessary
  100.     move.l    4.w,a6
  101.     moveq    #0,d0
  102.     moveq    #0,d1
  103.     jsr    _LVOSetSignal(a6)
  104.     move.l    dosbase(a5),a6
  105.     btst.l    #SIGBREAKB_CTRL_C,d0
  106.     beq.s    2$
  107.     error    BREAK
  108.     bra.s    .cleanup
  109.  
  110. 2$    addq.w    #1,column(a5)
  111.     cmp.w    #6,column(a5)
  112.     blt.s    1$
  113.     print    newline(pc)    ; write newline every 7 columns
  114.     clr.w    column(a5)
  115. 1$    bra.s    .return        ; return to outer loop
  116.  
  117. begin    dc.b    'Grepping memory for "%ls" from $%lx to $%lx:',10,0
  118. done    dc.b    10,'Grep finished, %ld hit(s).',10,0
  119. hit    dc.b    '$%-8lx   ',0
  120. newline=hit-2
  121. padpos=hit+2
  122.  
  123.